package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService detailService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> SpuPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "categoryId", required = false) Long cid,
            @RequestParam(value = "brandId", required = false) Long bid,
            @RequestParam(value = "saleable", required = false) Boolean saleable
    ) {


        return ResponseEntity.ok(this.spuService.SpuPage(page, rows, id, cid, bid, saleable));
    }

    /**
     * 新增商品
     *
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO) {
        this.spuService.addGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spuId查询商品信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {

        return ResponseEntity.ok(this.spuService.queryGoodsById(id));
    }

    /**
     * 修改 商品信息
     *
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {

        this.spuService.updateGoods(spuDTO);

        return ResponseEntity.ok().build();
    }

    /**
     * 根据id批量查询sku
     * @param ids id的集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuList(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**
     * 根据spuId查询对应的sku的集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuId(@RequestParam("id") Long id) {


        return ResponseEntity.ok(this.skuService.querySkuListBySpuId(id));
    }

    /**
     * 根据id查询SpuDetail
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailBySpuId(@RequestParam("id") Long id) {

        return ResponseEntity.ok(new SpuDetailDTO((detailService.getById(id))));
    }

    /**
     * 根据spuId查询spu的所有规格参数键值对
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamBySpuId(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching",required = false)Boolean searching
    ) {

        return ResponseEntity.ok(spuService.querySpecParamBySpuId(id,searching));
    }

    /**
     * 修改商品的上下架状态
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable
    ) {
        this.spuService.updateSaleable(id, saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id 查询 spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id
    ) {

        return ResponseEntity.ok(spuService.querySpuById(id));
    }

}
